package Interface;

import edu.uci.ics.jung.visualization.control.EditingModalGraphMouse;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import prim.Animator;

public class InterfaceBuilder {

    private static JFrame m_frame;
    private static final int SPEED_MIN = 3000;
    private static final int SPEED_MAX = 100;
    private static final int SPEED_INIT = 1500;

    private class KeyListener extends KeyAdapter
    {
        
    }
    
    public static void init() {
        m_frame = new JFrame("Prim");
        m_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        m_frame.setSize(new Dimension(700, 600));
        m_frame.setLocationRelativeTo(null);
        JMenuBar menuBar = new JMenuBar();

        // graph menu (load from file, randomize, crear) 
        menuBar.add(createGraphMenu());
        menuBar.add(createAlgorithmMenu());
        menuBar.add(createAnimationSpeedSlider());
        menuBar.add(createTimerBtn());

        m_frame.setJMenuBar(menuBar);

        m_frame.pack();
        m_frame.setVisible(true);
        //Animator.getInstance().init();
    }

    public static JFrame getMainFrame() {
        return m_frame;
    }

    private static JMenu createMouseModeMenu(EditingModalGraphMouse gm) {
        JMenu modeMenu = gm.getModeMenu();
        modeMenu.setText("Mouse Mode");
        modeMenu.setIcon(null);
        modeMenu.setPreferredSize(new Dimension(110, 20));
        return modeMenu;
    }

    private static JMenu createGraphMenu() {
        JMenu graphMenu = new JMenu("Граф");
        graphMenu.setIcon(null);
        graphMenu.setPreferredSize(new Dimension(70, 20));

        JMenuItem clearAction = new JMenuItem("Очистить");
        clearAction.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                Animator.getInstance().clear();
            }
        });

        //JMenuItem loadFromFileAction = new JMenuItem("Load from file");
        JMenuItem randomizeGraphAction = new JMenuItem("Создать случайный граф");
        randomizeGraphAction.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                Object[] numVertices = new Object[50];
                for (int i = 0; i < numVertices.length; i++) {
                    numVertices[i] = i;
                }

                Integer i = (Integer) JOptionPane.showInputDialog(
                        null,
                        "Сколько вершин создать?",
                        "",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        numVertices,
                        5);

                //Animator.getInstance().init();
                Animator.getInstance().setVerticesCount(i.intValue());
                Animator.getInstance().start(Animator.AnimationType.ADDING_VERTICES);
            }
        });

        graphMenu.add(clearAction);
        //graphMenu.add(loadFromFileAction);

        graphMenu.add(randomizeGraphAction);
        return graphMenu;
    }

    private static JMenu createAlgorithmMenu() {
        JMenu algorithmMenu = new JMenu("Алгоритм Прима");
        algorithmMenu.setIcon(null);
        algorithmMenu.setPreferredSize(new Dimension(140, 20));

        JMenuItem MSTAction = new JMenuItem("Найти минимальное остовное дерево");
        MSTAction.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                Animator.getInstance().start(Animator.AnimationType.PRIMS_ALGORITHM);
            }
        });

        algorithmMenu.add(MSTAction);
        //graphMenu.add(loadFromFileAction);

        algorithmMenu.add(MSTAction);
        return algorithmMenu;
    }

    private static JSlider createAnimationSpeedSlider() {
        JSlider slider = new JSlider(JSlider.HORIZONTAL, SPEED_MAX, SPEED_MIN, Animator.getInstance().getDelay());

        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider source = (JSlider) e.getSource();
                if (!source.getValueIsAdjusting()) {
                    Animator.getInstance().setDelay(source.getValue());
                }
            }
        });
        slider.setMajorTickSpacing(
                200);
        slider.setPaintLabels(
                true);
        slider.setLabelTable(slider.createStandardLabels(200));

        return slider;
    }

    private static JRadioButton createTimerBtn() {
        JRadioButton timerButton = new JRadioButton("Таймер");

        timerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
            }
        });

        return timerButton;
    }
}
